<?php

namespace Common\Decorator;

use Common\Interfaces\IDecorator;

class Decorator
{
    protected $iDecorators = array();

    public function index()
    {
        $this->beforeDecoratorIndex();
        echo "我是等待被装饰的文字<br>";
        $this->afterDecoratorIndex();
    }

    // 添加装饰器接口
    public function addIDecorator(IDecorator $iDecorator)
    {
        $this->iDecorators[] = $iDecorator;
    }

    // 装饰器实现过程
    public function beforeDecoratorIndex()
    {
        foreach ($this->iDecorators as $iDecorator) {
            $iDecorator->beforeDecoratorIndex();
        }
    }
    public function afterDecoratorIndex()
    {
        // 装饰器翻转 一定要翻转过来
        $iDecorators = array_reverse($this->iDecorators);
        foreach ($iDecorators as $iDecorator) {
            $iDecorator->afterDecoratorIndex();
        }
    }
}
